package com.attendance.controller;

import com.attendance.common.Result;
import com.attendance.dto.request.LoginRequest;
import com.attendance.dto.request.RegisterRequest;
import com.attendance.dto.request.UpdatePasswordRequest;
import com.attendance.dto.response.LoginResponse;
import com.attendance.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/auth")
public class AuthController {
    
    @Autowired
    private UserService userService;
    
    @PostMapping("/login")
    public Result<LoginResponse> login(@Valid @RequestBody LoginRequest request) {
        return Result.success(userService.login(request));
    }
    
    @PostMapping("/register")
    public Result<Void> register(@Valid @RequestBody RegisterRequest request) {
        userService.register(request);
        return Result.success();
    }
    
    @PostMapping("/updatePassword")
    public Result<Void> updatePassword(
        @Valid @RequestBody UpdatePasswordRequest request
    ) {
        userService.updatePassword(request, request.getUsername());
        return Result.success();
    }
    
    @PostMapping("/logout")
    public Result<Void> logout(Authentication authentication) {
        userService.logout(authentication.getName());
        return Result.success();
    }
} 